package edu.pku.sei.transformation.editor.views.tree;

import java.util.ArrayList;

import org.eclipse.jface.viewers.Viewer;

import com.sun.corba.se.impl.oa.poa.AOMEntry;

import edu.pku.sei.modeler.explorer.registry.ITreeViewContentProvider;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.transformation.editor.internal.TransformationEditorModelRoot;
import edu.pku.sei.transformation.editor.internal.TransformationEditorProject;
import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.GraphicalDomain;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.Transformation;

public class EditorTreeViewContentProvider implements ITreeViewContentProvider {

	public Object[] getChildren(Object parent) {
		if (parent instanceof MetaModel) {
			MetaModel metaModel = (MetaModel) parent;
			return metaModel.getPackages().toArray();
		} else if (parent instanceof MetaPackage) {
			MetaPackage metaPackage = (MetaPackage) parent;
			return getChildrenOfMetaPackage(metaPackage);
		} else if (parent instanceof Transformation) {
			Transformation trans = (Transformation) parent;
			return getChildrenOfTransformation(trans);
		} else if (parent instanceof Rule) {
			return getChildrenOfRule((Rule) parent);
		} else if (parent instanceof GraphicalDomain) {
			return getChildrenOfReltionDomain((GraphicalDomain) parent);
		} else if (parent instanceof MetaModel) {
			return ((MetaModel) parent).getPackages().toArray();
		}
		return new Object[0];
	}

	public Object getParent(Object child) {
		if (child instanceof MetaElement) {
			return ((MetaElement) child).getContainer();
		}
		return null;
	}

	private Object[] getChildrenOfRule(Rule relation) {
		ArrayList temp = new ArrayList();
		temp.addAll(relation.getVariables());
		temp.addAll(relation.getDomains());
		return temp.toArray();
	}

	private Object[] getChildrenOfReltionDomain(GraphicalDomain domain) {
		ArrayList temp = new ArrayList();
		temp.addAll(domain.getObjectVariables());
		temp.addAll(domain.getConnectionVariables());
		return temp.toArray();
	}

	private Object[] getChildrenOfTransformation(Transformation trans) {
		ArrayList temp = new ArrayList();
		if (trans instanceof AtomicTransformation) {
			temp.addAll(((AtomicTransformation)trans).getKey());
			temp.addAll(((AtomicTransformation)trans).getRules());			
		}
		return temp.toArray();
	}

	protected Object[] getChildrenOfMetaPackage(MetaPackage metaPackage) {
		ArrayList tempList = new ArrayList();
		tempList.addAll(metaPackage.getOtherElements());
		tempList.addAll(metaPackage.getNamedElements());
		tempList.addAll(metaPackage.getDiagrams());
		// tempList.addAll(metaPackage.getDiagrams());
		// tempList.addAll(metaPackage.getSubPackages());

		return tempList.toArray();
	}

	public boolean hasChildren(Object element) {
		// TODO Auto-generated method stub
		return false;
	}

	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		return null;
	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub

	}

	public String getNature() {
		return TransformationEditorProject.PROJECT_NATURE;
	}
}
